Skip to content

fix: guard against IndexError when repr() returns empty string in formatPair() - #240

Merged
Jakeroid merged 1 commit into
gruns:masterfrom
koteshyelamati:patch-1
Jul 6, 2026
Merged

fix: guard against IndexError when repr() returns empty string in formatPair()#240
Jakeroid merged 1 commit into
gruns:masterfrom
koteshyelamati:patch-1

Conversation

@koteshyelamati

Copy link
Copy Markdown
Contributor

Bug

formatPair() accesses value[0] unconditionally to check whether the stringified value looks like a Python string literal. When an object's __repr__ returns an empty string '', this raises IndexError: string index out of range whenever output is formatted across multiple lines (triggered by a long prefix, a long line, or a multiline value).

Reproduction

from icecream import ic

class EmptyRepr:
    def __repr__(self):
        return ''

ic.configureOutput(prefix='x' * 100)  # force multiline path
ic(EmptyRepr())  # IndexError: string index out of range

Fix

Add a len(value) >= 2 guard before the character-index access so that empty (and single-character) strings skip the string-literal check instead of crashing.

# Before
looksLikeAString = (value[0] + value[-1]) in ["''", '""']

# After
looksLikeAString = len(value) >= 2 and (value[0] + value[-1]) in ["''", '""']

formatPair() accessed value[0] unconditionally, crashing with IndexError
when an object's __repr__ returns an empty string and the output is
formatted across multiple lines (e.g. when the prefix is long).

Add a len(value) >= 2 guard before the index access.
@Jakeroid
Jakeroid merged commit 72312b8 into gruns:master Jul 6, 2026
8 checks passed
@Jakeroid

Jakeroid commented Jul 6, 2026

Copy link
Copy Markdown
Collaborator

@koteshyelamati

Thank you for the contribution!

Will you have time to add tests for that case?

@koteshyelamati

Copy link
Copy Markdown
Contributor Author

Thanks for merging @Jakeroid! Yes — I'll open a follow-up PR with tests for the empty and single-char repr cases.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants